import callback from './media-callback'
import photosCallBack from './photos.callback'
import callbackIosVideoUpload from './media-callback-iosvideo-upload'
import callbackForFileChooser from './openFileChooser-callback'
import downloadConfig from './download-callback'
import uploadCallBack from './upload.callBack'
// import savePhotoCallBack from './savePhoto.callBack'
/**
 * 功能： 调用相册选择图片
 * @params {String} format 打开哪种内容的相册(仅ios支持) image-图片 ?-视频
 * @params {String} maxCount 最多几张
 * @params {String} remainCount 还剩几张
 * @params {String} params 业务json string(仅ios支持)
 */
export const selectPhoto = {
  ios: 'xblOpenAlbum',
  android: 'selectPhotos',

  params: {
    format: 'image',
    maxCount: 10,
    remainCount: 10,
    localHandle: 'y'
  },

  paramsHandler (params) {
    if (this._os === 'android') {
      return [params.maxCount, params.remainCount]
    } else if (this._os === 'ios') {
      return [params]
    }
  },

  callback
}

/**
 * 功能：将base64保存到本地
 * @params {String} format 打开哪种内容的相册(仅ios支持) image-图片 ?-视频
 */
export const savePhoto = {
  ios: 'saveImgToPhoto',
  android: 'selectPhotos',

  params: {
    imgStr: ''
  },

  paramsHandler (params) {
    if (this._os === 'android') {
      return [params.imgStr]
    } else if (this._os === 'ios') {
      return [params]
    }
  },

  callback
}
/**
 * 功能： 新的原生调用相册选择图片
 * @params {String} format 打开哪种内容的相册(仅ios支持) image-图片 ?-视频
 * @params {String} maxCount 最多几张
 * @params {String} remainCount 还剩几张
 * @params {String} params 业务json string(仅ios支持)
 */
export const selectPhotoV2 = {
  ios: 'xblSelectPhotoV2',
  android: 'selectPhotosV2',
  params: {
    format: 'image',
    maxCount: 10,
    remainCount: 10
  },

  paramsHandler (params) {
    if (this._os === 'android') {
      return [params.maxCount, params.remainCount]
    } else if (this._os === 'ios') {
      return [params]
    }
  },

  callback: photosCallBack
}
/**
 * 功能：调用ios原生选择视频(ios自己上传)
 */
export const selectVideoUpload = {
  ios: 'xblSelectVideoUpload',
  android: 'xblSelectVideoUpload',
  params: {
    acToken: '',
    branchCode: '',
    uploadUrl: '',
    fileMax: window.$app.fileMaxSize['mp4'] || '300'
  },

  paramsHandler (params) {
    if (this._os === 'android') {
      return [params.duration]
    } else if (this._os === 'ios') {
      params.fileMax = window.$app.fileMaxSize['mp4'] || '300'
      params.acToken = window.$app.user.token
      params.branchCode = window.$app.user.branchCode
      return [params]
    }
  },
  callback: uploadCallBack
}
/**
 * 功能：调用原生选择视频
 */
// export const selectVideo = {
//   ios: 'selectVideoiOS',
//   android: 'selectVideo',

//   params: {
//     localHandle: 'y'
//   },

//   paramsHandler(params) {
//     if (this._os === 'android') {
//       return []
//     } else if (this._os === 'ios') {
//       return [params]
//     }
//   },

//   callback
// }
/**
 * 功能：调用原生选择视频
 */
export const selectVideo = {
  // ios: 'xblSelectVideoWithMaxDuration',
  // ios: 'xblSelectVideoUpload',
  ios: 'xblSelectVideoUpload',
  android: 'selectVideoWithDuration',

  params: {
    uploadUrl: window.$app.fileUploadUrl,
    fileMax: (window.$app.fileMaxSize && window.$app.fileMaxSize['mp4']) || '300',
    localHandle: 'y',
    maxDuration: '20',
    duration: '20'
  },

  paramsHandler (params) {
    if (this._os === 'android') {
      return [params.duration]
    } else if (this._os === 'ios') {
      params.fileMax = (window.$app.fileMaxSize && window.$app.fileMaxSize['mp4']) || '300'
      params.acToken = window.$app.user.token
      params.branchCode = window.$app.user.branchCode
      return [params]
    }
  },

  callback: (window.webkit && window.webkit.messageHandlers) ? callbackIosVideoUpload : callback
}
/**
 * 功能：调用原生选择视频
 */
export const openCamera = {
  ios: 'openCamera',
  android: 'openCamera',

  params: {
    cameraType: '2',
    hdMode: '0',
    hideMode: '0',
    acToken: '',
    branchCode: '',
    uploadUrl: '',
    fileMax: '300',
    ip: '',
    port: '',
    isAIWorkType: ''
  },

  // paramsHandler (params) {
  //   if (this._os === 'android') {
  //     return [params.duration]
  //   } else if (this._os === 'ios') {
  //     return [params]
  //   }
  // },

  callback: uploadCallBack
}

/**
 * 功能：调用原生选择视频
 */
export const openMobileCamera = {
  ios: 'openCamera',
  android: 'openCamera',

  params: {
    acToken: '',
    branchCode: '',
    uploadUrl: '',
    cameraType: '2',
    hdMode: '1',
    localHandle: 'y',
    localCrop: 'y',
    verticalScreen: '1',
    fileMax: ''
  },

  paramsHandler (params) {
    if (this._os === 'android') {
      return [params.duration]
    } else if (this._os === 'ios') {
      return [params]
    }
  },

  callback: uploadCallBack
}

export const selectMobilePhoto = {
  ios: 'xblOpenAlbum',
  android: 'selectPhotosWithCrop',

  params: {
    format: 'image',
    maxCount: 10,
    remainCount: 10,
    localHandle: 'y',
    localCrop: 'y'
  },

  paramsHandler (params) {
    if (this._os === 'android') {
      return [params.maxCount, params.remainCount]
    } else if (this._os === 'ios') {
      return [params]
    }
  },

  callback
}

export const selectMobileVideo = {
  ios: 'xblSelectVideoUpload',
  android: 'selectVideoWithDuration',

  params: {
    uploadUrl: window.$app.fileUploadUrl,
    fileMax: (window.$app.fileMaxSize && window.$app.fileMaxSize['mp4']) || '300',
    localHandle: 'y',
    maxDuration: '20',
    duration: '20'
  },

  paramsHandler (params) {
    if (this._os === 'android') {
      return [params.duration]
    } else if (this._os === 'ios') {
      params.fileMax = (window.$app.fileMaxSize && window.$app.fileMaxSize['mp4']) || '300'
      params.acToken = window.$app.user.token
      params.branchCode = window.$app.user.branchCode
      return [params]
    }
  },

  callback: uploadCallBack
}

/**
 * 功能：调用原生拍照
 */
export const takePhoto = {
  ios: 'xblOpenCamera',
  android: 'takePhoto',

  params: {
    localHandle: 'y'
  },

  paramsHandler (params) {
    if (this._os === 'android') {
      return []
    } else if (this._os === 'ios') {
      return [params]
    }
  },

  callback
}

/**
 * 功能：调用原生录像
 */
export const takeVideo = {
  ios: 'takeVideoiOS',
  android: 'takeVideo',

  params: {
    localHandle: 'y'
  },

  paramsHandler (params) {
    if (this._os === 'android') {
      return []
    } else if (this._os === 'ios') {
      return [params]
    }
  },

  callback
}

/**
 * 功能：调用原生录音
 */
export const takeAudio = {
  ios: 'xblOpenAudio',
  android: 'takeVoice',

  params: {
    maxTime: 300,
    localHandle: 'y'
  },

  paramsHandler (params) {
    if (this._os === 'android') {
      return []
    } else if (this._os === 'ios') {
      return [params]
    }
  },

  callback
}

/**
 * 功能： 调用签字板
 * @params {Any} extendParam 在调用后会回传回来
 */
export const takeSign = {
  ios: 'xblOpenSign',
  android: 'openSignature',

  params: {
    extendParam: '',
    localHandle: 'y'
  },

  paramsHandler (params) {
    if (this._os === 'android') {
      return [params.extendParam]
    } else if (this._os === 'ios') {
      return [params]
    }
  },

  callback
}

/**
 * 功能：调用分享功能
 */
export const share = {
  ios: 'xblShare',
  android: 'xblShare',

  params: {
    link: '',
    title: '',
    thumbnail: '',
    description: '',
    content: '',
    extendParam: '',
    localHandle: 'y'
  },

  paramsHandler (params) {
    if (this._os === 'android') {
      if (!params.content) {
        params.content = params.description
      }

      return [params.link, params.title, params.thumbnail, params.description, params.content, params.extendParam]
    } else if (this._os === 'ios') {
      return [params]
    }
  }
}

/**
 * 功能：调用原生打印
 */
export const print = {
  ios: 'xblAirPrint',
  android: 'print',
  ...downloadConfig
}

/**
 * 功能：调用原生扫描
 */
export const scan = {
  ios: 'xblOpenScanCamera',
  android: 'openScanCamera',

  params: {
    scanCallback: null,
    extendParam: '',
    localHandle: 'y'
  },

  before () {
    /**
     * 功能：扫描回调函数
     * @params {String} scanResult 扫描切图结果的json字符串
     */
    let me = this
    window.xblScanFinish = function (scanResult) {
      if (me.params.scanCallback) {
        me.params.scanCallback(scanResult)
      }
    }
  },

  paramsHandler (params) {
    if (this._os === 'android') {
      return [params.extendParam]
    } else if (this._os === 'ios') {
      return [{
        extendParam: params.extendParam,
        localHandle: 'y'
      }]
    }
  },

  callback
}

/**
 * 功能：调用原生下载
 */
export const download = {
  ios: 'xblDownloadFile',
  android: 'downloadFile',
  ...downloadConfig
}

/**
 * 功能：取消原生下载
 */
export const cancelDownload = {
  ios: 'xblCancelDownloadFile',
  android: 'cancelDownloadFile',
  ...downloadConfig
}

/**
 * 功能： 切换横竖屏
 * @params {String} userInfo (仅android需要) 传入本地的自定义信息,可以是json字符串,
 *  在完成本地方法后，在本地调用webview 的xblSelectFinish 将回传此字段
 * @params {String} orientation '0'-竖屏 '1'-横屏 '2'-自动感应
 */
export const changeOrientation = {
  params: {
    userInfo: '',
    orientation: '1'
  },

  paramsHandler (params) {
    if (this._os === 'android') {
      return [params.userInfo, params.orientation]
    } else if (this._os === 'ios') {
      return [{ orientation: params.orientation }]
    }
  }
}
/**
 * 功能: 安卓打开文件存储
 */
export const selectFile = {
  android: 'openFileChooser',

  params: {
    userInfo: ''
  },

  paramsHandler (params) {
    if (this._os === 'android') {
      return [params.userInfo]
    }
  },

  callback: callbackForFileChooser
}
/**
 * 功能: 安卓打开文件存储
 */
export const selectFiles = {
  android: 'openFileChooserWithFileTypes',

  params: {
    fileTypes: '',
    userInfo: ''

  },

  paramsHandler (params) {
    if (this._os === 'android') {
      return [params.fileTypes, params.userInfo]
    }
  },
  callback: callbackForFileChooser
}
/**
 * 功能：打开文件
 */
export const openFile = {
  ios: 'xblOpenFileOnWeb',
  android: 'openFile',

  params: {
    url: '',
    fileName: '',
    userInfo: ''
  },

  paramsHandler (params) {
    if (this._os === 'android') {
      return [params.url, params.fileName, params.userInfo]
    } else if (this._os === 'ios') {
      return [params]
    }
  }
}
/**
 * 功能：打开链接
 */
export const openLink = {
  ios: 'xblOpenUrlInner',
  android: 'openUrl',

  params: {
    url: '',
    title: '',
    hasBar: '0',
    userInfo: ''
  },

  paramsHandler (params) {
    if (this._os === 'android') {
      return [params.url, params.title, params.hasBar, params.userInfo]
    } else if (this._os === 'ios') {
      return [params]
    }
  }
}

/**
 * 功能：打开文件带返回
 */
export const openFileWithBack = {
  ios: 'openFileWithBack',
  android: 'openFileWithBack',

  params: {
    url: '',
    fileName: '',
    userInfo: ''
  },

  paramsHandler (params) {
    if (this._os === 'android') {
      return [params.url, params.fileName, params.userInfo]
    } else if (this._os === 'ios') {
      return [params]
    }
  },

  callback: {
    name: 'openFileBack',
    /**
     * 下载回调后的参数处理
     * @param {String} status 0 打开失败， 1 打开成功
     */
    handler (option, status) {
      return {
        status
      }
    }
  }
}

/**
 * 功能：调用ios原生选择视频(ios自己上传)
 */
export const selectPhotoAndVideo = {
  ios: 'selectPhotoAndVideo',
  android: 'selectPhotoAndVideo',
  params: {
    acToken: '',
    branchCode: '',
    uploadUrl: '',
    fileMax: '300',
    maxCount: 10,
    remainCount: 10
  },
  paramsHandler (params) {
    if (this._os === 'android') {
      return [params.maxCount, params.remainCount]
    } else if (this._os === 'ios') {
      return [params]
    }
  },
  callback: uploadCallBack
}

/**
 * 功能：检测文件是否已经下载
 */
export const checkFileExist = {
  ios: 'checkFileExist',
  android: 'checkFileExist',

  params: {
    url: '',
    fileName: ''
  },

  paramsHandler (params) {
    if (this._os === 'android') {
      return [params.url, params.fileName]
    } else if (this._os === 'ios') {
      return [params]
    }
  },

  callback: {
    name: 'checkFileExistBack',
    /**
     * 下载回调后的参数处理
     * @param {String} status 0 未下载， 1 已下载
     */
    handler (option, status) {
      console.log('checkFileExistBack status:', status, option)
      return {
        status
      }
    }
  }
}

/**
 * 功能：检测文件是否已经下载
 */
export const clearInputUploadFiles = {
  ios: 'clearInputUploadFiles',
  android: 'clearInputUploadFiles',

  paramsHandler () {
    if (this._os === 'android') {
      return []
    } else if (this._os === 'ios') {
      return []
    }
  }
}

/**
 * 功能：打开office文件，支持提交
 */
export const openOfficeWithEdit = {
  ios: 'openOfficeWithEdit',
  android: 'openOfficeWithEdit',

  params: {
    url: '',
    fileName: ''
  },

  paramsHandler (params) {
    if (this._os === 'android') {
      return [params.url, params.fileName]
    } else if (this._os === 'ios') {
      return [params]
    }
  },

  callback: {
    name: 'openOfficeEditBack',
    /**
     * 下载回调后的参数处理
     * @param {String} status 0 未下载， 1 已下载
     * * @param {String} submitFilePath  文件地址
     */
    handler (option, status, submitFilePath) {
      return {
        status,
        submitFilePath
      }
    }
  }
}

/**
 * 功能： 显示或隐藏截屏图标
 */
export const showOrHideScreenShot = {
  ios: 'showOrHideScreenShot',
  android: 'showOrHideScreenShot',

  params: {
    show: ''
  },

  paramsHandler (params) {
    if (this._os === 'android') {
      return [params.show]
    } else if (this._os === 'ios') {
      return [params]
    }
  }

}

/**
 * 功能： 设置授课状态
 *  @param status  0-非授课   1-授课
 */
export const setTeachingStatus = {
  ios: 'setTeachingStatus',
  android: 'setTeachingStatus',

  params: {
    status: ''
  },

  paramsHandler (params) {
    if (this._os === 'android') {
      return [params.status]
    } else if (this._os === 'ios') {
      return [params]
    }
  }
}

export const getDeviceModel = {
  ios: 'getDeviceModel',
  android: 'getDeviceModel',

  params: {

  },

  paramsHandler (params) {
    if (this._os === 'android') {
      return []
    } else if (this._os === 'ios') {
      return [params]
    }
  },

  callback: {
    name: 'padDeviceModel',
    handler (option, padDeviceModel) {
      return {
        padDeviceModel
      }
    }
  }
}
